<%--
  Created by IntelliJ IDEA.
  User: hc
  Date: 14-5-15
  Time: 下午16:10
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
    <%@include file="../../common/include_list_head.jsp" %>
    <title></title>

    <script type="text/javascript">

        var idRoles = "";
        function CheckedInfo(obj) {

            var role = $("#Role_" + obj).val();
            if ($("#Role_" + obj).attr("checked")) {
                idRoles = idRoles + "," + role;
            } else {
                if ("" != idRoles) {
                    var msg = "";
                    var roles = idRoles.split(",");
                    for (var j = 0; j < roles.length; j++) {
                        if (roles[j] != role) {
                            msg = "," + roles[j];
                        }
                    }

                    idRoles = msg;
                }
            }

            window.parent.document.getElementById("rolesId").value = idRoles;
//            alert(""+document.getElementById("appid").value);
            window.parent.document.getElementById("appid").value = document.getElementById("appid").value;
        }
    </script>
    <script type="text/javascript">

        $(document).ready(function () {
            window.parent.document.getElementById("rolesId").value = null;
            //菜单按层次向后两个空格
            $("td[id^='td_']").each(function (i) {
                var id_len_3 = $(this).attr("id").length / 3 - 2;
                var str = "";
                if (id_len_3 > 0) {
                    for (var i = 0; i < id_len_3; i++) {
                        str += "&nbsp;&nbsp;&nbsp;";
                    }
                }
                $(this).html(str + $(this).html());
            });
            window.parent.document.getElementById("menues").value = null;
//            //初始化选中 已选中的菜单
            //得到所有拥有门户权限的集合
//            var role = window.parent.document.getElementById("rolesId").value;
//            alert(""+role);
            var roles = window.parent.document.getElementById("roles").value;
            if (roles != null || roles != "") {
                var rolebuffer = document.getElementById("rolebuffer").value;
                var roles = roles.split(",");
                var rolebuffer = rolebuffer.split(",");
                for (var j = 0; j < rolebuffer.length; j++) {
                    for (var i = 0; i < roles.length; i++) {
                        if (rolebuffer[j] == roles[i]) {
                            $("#Role_" + roles[i]).attr("checked", "checked");
                            CheckedInfo(rolebuffer[j]);
                        }
                    }
                }
            }

            //            $("input[id^='Role_']").each(function (i) {
            //            });
            //查看状态保存按钮隐藏 输入框只读等
            //            doReadOnly();
        })
        ;
        //更新菜单信息
        function tochangeMenu(obj) {
            if (obj.checked) { //菜单被选中时

                //菜单选中时其上级菜单默认选中
                var len_3 = obj.value.length / 3;
                for (var i = 2; i < len_3; i++) {
                    var str = obj.value.substr(0, i * 3);
                    if (!$("#menu_" + str).attr("checked")) { //上级菜单未选中时
                        $("#menu_" + str).attr("checked", "checked");
                        var menues = window.parent.document.getElementById("menues").value;
                        if (menues == null || menues == '') {
                            window.parent.document.getElementById("menues").value = $("#menu_" + str).val();
                        } else {
                            window.parent.document.getElementById("menues").value += "," + $("#menu_" + str).val();
                        }
                    }
                }
                //菜单选中时其下级菜单默认选中
                $("input[id^='menu_" + obj.value + "']").each(function (i) {
                    if (!this.checked) { //上级菜单未选中时
                        $(this).attr("checked", "checked");
                        var menues = window.parent.document.getElementById("menues").value;
                        if (menues == null || menues == '') {

                            window.parent.document.getElementById("menues").value = $(this).val();
                        } else {

                            window.parent.document.getElementById("menues").value += "," + $(this).val();
                        }
                    }
                });
                //处理本级菜单
                var menues = window.parent.document.getElementById("menues").value;

                if (menues == null || menues == '') {
                    window.parent.document.getElementById("menues").value = obj.value;
                } else {
                    window.parent.document.getElementById("menues").value += "," + obj.value;
                }
            } else { //菜单取消选中时
                //下级菜单取消选中
                $("input[id^='menu_" + obj.value + "']").attr("checked", "");
                //更新菜单信息
                var menues = window.parent.document.getElementById("menues").value;
                var menue = menues.split(",");
                var curr_menues = "";
                for (var i = 0; i < menue.length; i++) {
                    if (menue[i].indexOf(obj.value) != 0) {
                        curr_menues += menue[i] + ",";
                    }
                }
                if (curr_menues.lastIndexOf(",") == curr_menues.length - 1) {
                    curr_menues = curr_menues.substr(0, curr_menues.length - 1);
                }

                window.parent.document.getElementById("menues").value = curr_menues;
            }
        }
        function changeAll(obj) {
            window.parent.document.getElementById("menues").value = null;
            if (!obj.checked) {
                $("input[name^='selectNodeMenu']").each(function (i) { //二级菜单取消选中时,下级菜单全部取消选中
                    if (this.checked) { //该菜单的按钮已选中
                        $(this).attr("checked", ""); //选中菜单下的按钮后默认选中该菜单
                    }
                });
                //更新菜单信息
                var menues = window.parent.document.getElementById("menues").value;

                var menue = menues.split(",");
                var curr_menues = "";
                for (var i = 0; i < menue.length; i++) {
                    if (menue[i].indexOf(obj.value) != 0) {
                        curr_menues += menue[i] + ",";
                    }
                }

                if (curr_menues.lastIndexOf(",") == curr_menues.length - 1) {
                    curr_menues = curr_menues.substr(0, curr_menues.length - 1);
                }
                window.parent.document.getElementById("menues").value = "";
//                window.parent.document.getElementById("menues").value = curr_menues;
            } else { //选中二级菜单时,默认其下级菜单全部选中
                $("input[name='selectNodeMenu']").each(function (i) { //二级菜单取消选中时,下级菜单全部取消选中
                    $(this).attr("checked", "checked"); //选中菜单下的按钮后默认选中该菜单
                    menues = window.parent.document.getElementById("menues").value;
                    if (menues == null || menues == '') {
                        window.parent.document.getElementById("menues").value = this.value;
                    } else {
                        window.parent.document.getElementById("menues").value += "," + this.value;

                    }
                });
            }
        }
    </script>
</head>
<body style="height: 600px;">
<div style="height: 400px;">
    <s:if test="actionErrors.size()>0 || actionMessages.size()>0 || fieldErrors.size()>0">
        <div id="SystemErrorMessage">
            <s:actionerror/>
            <s:actionmessage/>
            <s:fielderror/>
        </div>
    </s:if>
    <s:hidden id="menues" name="menues"/>
    <div style="width:100%;min-height:60px;height: auto;">
        <s:iterator value="rolelist" status="status">
            <div style="margin-top: 20px;">
                <li style="float: left;width: 20%">
                    <input type="checkbox" id="Role_<s:property value="roleid"/>" name="selectNode"
                           value="<s:property value="roleid"/>"
                           onclick="CheckedInfo('<s:property value="roleid"/>');"/>
                    <a href="javascript:openEntity('<s:property value="roleid"/>')">
                        <font color="blue"><s:property value="rolename"/></font></a>
                    <s:hidden name="appid" id="appid" value="%{appid}"/>
                    <s:hidden name="rolebuffer" id="rolebuffer" value="%{rolebuffer}"/>
                </li>
            </div>
        </s:iterator>
    </div>

    <div style="height:390px;width:100%;" id="scrollContent">
        <s:hidden id="roleids" value="%{roleids}"/>
        <table class="tableStyle" headFixMode="true" id="tab" style="font-size: 12px">
            <tr>
                <td align="left" colspan="100">
                    <s:checkbox id="menu_%{taccessAppRegid}" name="selectNodeMenu" fieldValue="%{taccessAppRegid}"
                                onclick="changeAll(this)"/>
                    <label for="menu_<s:property value="upmenuid"/>"><s:property value="menuname"/></label>
                    <s:hidden name="upmenuid"/><s:hidden name="roleid"/>
                </td>
            </tr>
            <tr>
                <th align="center" width="20%">资源名称</th>
                <th align="center" width="15%">所属系统</th>
                <th align="center" width="15%">资源状态</th>
                <th align="center" width="15%">资源类型</th>
                <th align="center" width="15%">编号</th>
                <th align="center" width="30%">资源路径</th>

            </tr>

            <s:iterator value="menulist" id="lm" status="i">
                <tr>

                    <td width="180" id="td_<s:property value='menuid'/>">

                        <s:checkbox id="menu_%{menuid}" name="selectNodeMenu" fieldValue="%{menuid}"
                                    onclick="tochangeMenu(this)"/>
                        <label for="menu_<s:property value="%{menuid}" />"><s:property value="menuname"/>
                            <span id="span_<s:property value='%{#i.index}'/>"></span></label>
                    </td>

                    <td align="center">
                        <s:property value="portalname"/>

                    </td>
                    <td align="center">
                        <s:if test="state==0">禁用</s:if>
                        <s:else>可用</s:else>

                    </td>
                    <td align="center">
                        <s:property value="ywxtmc"/></td>
                    <td align="center">
                        <s:property value="bizcode"/></td>
                    <td align="center">
                        <s:property value="menupath"/>
                    </td>
                </tr>
            </s:iterator>
        </table>
    </div>
</div>
</body>
</html>